/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.jsimplemarket.model;

import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
 *
 * @author Alexandru.Codescu
 */
@Entity
@Table(name = "plati")
@NamedQueries({
    @NamedQuery(
        name = "Plata.querySumIncasariByDay",
        query = "select c.dataInceput, sum(p.valoare) "
                + "from Plata p "
                + "join p.creanta c "
                + "where c.dataInceput > ?1 "
                + "and c.taxa.tipTaxa.id = 1 "
                + "group by c.dataInceput" +
                " order by c.dataInceput desc"),
    @NamedQuery(
        name = "Plata.querySumIncasariByMonth",
        query = "select monthname(c.dataInceput), sum(p.valoare), c.dataInceput "
                + "from Plata p "
                + "join p.creanta c "
                + "where c.dataInceput >= ?1 "
                //+ "and c.taxa.id not in (2,3) "
                + "group by monthname(c.dataInceput)" +
                " order by c.dataInceput asc"),
                
    @NamedQuery(
        name = "Plata.querySumIncasariByDayFromTaxe",
        query = "select c.dataInceput, sum(p.valoare) "
                + "from Plata p "
                + "join p.creanta c "
                + "where c.dataInceput > ?1 "
                + "and c.taxa.tipTaxa.id = 1 "
                + "group by c.dataInceput"),
    @NamedQuery(
        name = "Plata.queryIncasariByDataPlata",
        query = "select tt.denumire, t.valoare, count(c), p.dataPlata "
                + "from Plata p "
                + "join p.creanta c "
                + "join c.taxa t "
                + "join t.tipTaxa tt "
                + "where c.dataInceput <> p.dataPlata "
                + "and c.dataInceput = ?1 "
                //+ "and i.dataSfarsit <= ?2 "
                + "group by tt.denumire, t.valoare")
})

public class Plata extends AbstractEntitate {
    private Date dataPlata;
    private BigDecimal valoare;
    private Creanta creanta;

    @Column(name = "DATA_PLATA", nullable = false)
    @Temporal(TemporalType.DATE)
    public Date getDataPlata() {
        return dataPlata;
    }

    public void setDataPlata(Date dataPlata) {
        this.dataPlata = dataPlata;
    }

    @Column(name = "VALOARE", nullable = false)
    public BigDecimal getValoare() {
        return valoare;
    }

    public void setValoare(BigDecimal valoare) {
        this.valoare = valoare;
    }

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
//    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
    @JoinColumn(name = "CREANTA_ID")
    public Creanta getCreanta() {
        return creanta;
    }

    public void setCreanta(Creanta creanta) {
        this.creanta = creanta;
    }
}
